#include "../header.h"

int main(int argc, char const *argv[])
{
    pid_t pid = fork();
    if (pid < 0)
        exit(-1);
    if(pid > 0)
        exit(0);
    if(pid == 0)
    {
        pid_t sid = setsid();
        printf("新会话组的组长进程sid = %d  子进程pid = %d\n", sid,getpid());
        if( umask(0666 < 0))
        {
            perror("umask set error\n");
            return -1;
        }
        printf("权限掩码设置成功\n");
        if(mkdir("one",0775) < 0)
        {
            perror("创建新工作目录失败\n");
            return -1;
        }
        if(chdir("./one") < 0)
        {
            perror("修改工作目录失败\n");
        }
        printf("工作目录修改成功\n");
        int fd = open("/dev/null",O_WRONLY);
        if(fd < 0)
        {
            perror("open /dev/nullerror\n");
            return -1;
        }
        dup2(fd,STDOUT_FILENO);
        dup2(fd,STDIN_FILENO);
        dup2(fd,STDERR_FILENO);
        close(fd);
        time_t tm = 0;
        int fdm = open("info.time",O_CREAT|O_WRONLY|O_APPEND,0666);
        char *stime = NULL;
        while(1)
        {
            tm = time(NULL);
            stime = asctime(localtime(&tm));
            write(fdm,stime,strlen(stime));
            sleep(1);
        }
        close(fdm);
    }
    return 0;
}
